//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    letterList: [],
    page: 1,
    last_page: null,
    total: null,
    showBox: false,
    helpModal:false
  },

  onLoad: function () {
    let that = this

    console.log(app.globalData.dbInfo)

    app.httpGet('index/getLetterListByPage',
      {
        openid: app.globalData.dbInfo.openid,
        id: app.globalData.dbInfo.id,
        page: 1
      },
      function (res) {

        console.log(res)

        that.setData({
          letterList: res.list,
          page: res.page,
          last_page: res.last_page,
          total: res.total
        })
      })

  },
  showHelp(){
    this.setData({
      helpModal:true
    })
  },
  hideHelp(){
    this.setData({
      helpModal:false
    })
  },
  //上拉触底获取更多
  onReachBottom() {
    console.log('触底了')
    let that = this
    if (that.data.page < that.data.last_page) {
      console.log('加载')

      app.httpGet('index/getLetterListByPage',
        {
          openid: app.globalData.dbInfo.openid,
          id: app.globalData.dbInfo.id,
          page: that.data.page + 1
        },
        function (res) {
          wx.stopPullDownRefresh();

          var letter_list = that.data.letterList

          letter_list.push.apply(letter_list, res.list);

          that.setData({
            letterList: letter_list,
            page: res.page,
            last_page: res.last_page,
            total: res.total
          })
        }, false)
    }

  },
  //前往详情
  toDetail(e){
    wx.navigateTo({
      url:"/pages/mine/detail/detail?index="+e.currentTarget.dataset.index
    })
  },

  //下拉刷新
  onPullDownRefresh(e) {
    let that = this
    app.httpGet('index/getLetterListByPage',
      {
        openid: app.globalData.dbInfo.openid,
        id: app.globalData.dbInfo.id,
        page: 1
      },
      function (res) {
        wx.stopPullDownRefresh();

        that.setData({
          letterList: res.list,
          page: res.page,
          last_page: res.last_page,
          total: res.total
        })
      }, false)
  },
  onShareAppMessage: function () {

  
    qq.showShareMenu({
      showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
    })
  
  
    return {
   
    
        imageUrl: '/images/logo.png',        
  		path: "/pages/index/index"
    }
  
  }

})
